package com.etoak.stock.mq;

import com.etoak.stock.service.StockService;
import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author by 孙有泽
 * @Date 2024/10/1421:53
 * @Description 定义股票相关mq消息监听
 */
@Component
@Slf4j
public class StockMQMsqListener {
    @Autowired
    private Cache<String,Object> caffeineCache;
    @Autowired
    private StockService stockService;
    @RabbitListener(queues = "innerMarketQueue")
    public void refreshInnerMarketInfo(Date data){
        //统计当前时间点与发送消息时间点的差值
        long diffTime = DateTime.now().getMillis() - new DateTime(data).getMillis();
        if(diffTime>60000){
           log.error("大盘发送消息时间:{},延迟毫秒数:{}",new DateTime(data).toString("yyyy-MM-dd HH:mm:ss"),diffTime);
        }
        //刷新缓存
        caffeineCache.invalidate("innerMarketInfos");
        //调佣服务方法 刷新数据
        stockService.getInnerMarketInfo();


    }
}
